package com.moco.launcher.fragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.moco.launcher.R;
import com.moco.launcher.alarm.Constant;
import com.moco.launcher.bean.PickItemInfo;
import com.moco.launcher.view.PickerView;
import com.moco.launcher.view.PickerView.UiParam;
import com.moco.launcher.view.PickerView.onSelectListener;
import com.oazon.common.util.Utils;

public class SleepWaitFragment extends BaseFragment {

	private static final String TAG = "SleepWaitFragment";
	private SharedPreferences mConfig;
	private Button mButton = null;
	private List<PickItemInfo> data = new ArrayList<PickItemInfo>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		for (int i = 15; i <= 60;) {
			PickItemInfo item = new PickItemInfo();
			item.setText(Integer.toString(i));
			i += 15;
			data.add(item);
		}
		mConfig = getContext().getSharedPreferences(Utils.CONFIG_FILE,
				getContext().MODE_PRIVATE);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.sleepwait_layout, container,
				false);
		final PickerView pv = (PickerView) rootView
				.findViewById(R.id.time_choice);
		pv.setOnSelectListener(new onSelectListener() {

			@Override
			public boolean onSelect(String nSelectId) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			public boolean onStop(String nSelectId) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean onRelayout(String nSelectId, UiParam param) {
				// TODO Auto-generated method stub
				param.HlColor = 0xee7b1d;
				param.nMarginWidth = SleepWaitFragment.this.getActivity()
						.getResources()
						.getDimension(R.dimen.sleep_view_margin_width);

				return true;
			}

		});
		pv.setData(data);

		ImageView sl_exit = (ImageView) rootView.findViewById(R.id.sl_exit);
		sl_exit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (ml != null) {
					ml.finishActivity();
				}
			}

		});
		ImageView choice = (ImageView) rootView.findViewById(R.id.sl_choice);
		choice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Editor edit = mConfig.edit();
				edit.putString("sleep_wait", pv.getCurrentSelectText());
				edit.commit();
				sendActionListener(0,
						Integer.parseInt(pv.getCurrentSelectText()));
				if (ml != null) {
					ml.finishActivity();
				}
			}
		});
		return rootView;
	}

	private void sendActionListener(int hour, int minute) {
		AlarmManager alarmManager = (AlarmManager) getActivity()
				.getSystemService(Context.ALARM_SERVICE);
		Calendar calendar = Calendar.getInstance(Locale.getDefault());
		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		Intent intent = new Intent();
		intent.setAction("com.sleep.delay.handle.action");
		intent.putExtra(Constant.SLEEPDELAY_KEY, Constant.SLEEP_PWERSLEEP);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(),
				0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
				calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
				pendingIntent);
	}
}
